<template>
	<view class="u-p-30">
		<view class="u-r-16 u-bg-fff u-p-30 u-flex-y-center" @click="$u.route('/pages/member/address/address',{type:1})" v-if="cart.address.id>0">
			<image src="../../static/address.png" style="width: 48rpx;height: 48rpx;"></image>
			<view class="u-flex-1" style="padding: 0 30rpx;">
				<view class="u-f-28 u-bold">{{cart.address.area}} {{cart.address.address}}</view>
				<view class="u-f-28 u-m-t-10" style="color: #666;">{{cart.address.username}} {{cart.address.mobile}}</view>
			</view>
			<u-icon name="arrow-right"></u-icon>
		</view>
		<view class="u-r-16 u-bg-fff u-p-30 u-flex-y-center" @click="$u.route('/pages/member/address/address',{type:1})" v-else>
			<image src="../../static/address.png" style="width: 48rpx;height: 48rpx;"></image>
			<view class="u-flex-1" style="padding: 0 30rpx;">
				<view class="u-f-28 u-bold">请选择收货地址</view>
			</view>
			<u-icon name="arrow-right"></u-icon>
		</view>
		<u-gap height="30rpx"></u-gap>
		<view class="u-r-16 u-bg-fff u-p-30">
			<block v-for="(item,index) in cart.cart"> 
			
			<view class="u-flex-y-center">
				<image :src="item.image" style="width: 118rpx;height: 118rpx;border-radius: 20rpx;"></image>
				<!-- <view class="u-flex-1 u-m-l-30">
					<view class="u-flex-y-center u-flex-between">
						<view class="u-f-28 u-bold u-line-1">{{item.name}}</view>
						
					</view>
					<view class="u-f-24 u-m-t-10" style="color: #999999;">{{item.attr_name}}</view>
					<u-gap height="40rpx"></u-gap>
					<view class="u-flex-y-center u-flex-between">
						<view style="color: #ED361D;">
							<text class="u-f-24">¥</text><text class="u-f-36 u-bold">{{item.price}}</text>
						</view>
					</view>
				</view>
				 -->
				<view class="u-m-l-16">
					<view class="u-f-28 u-bold u-m-b-14">{{item.name}}</view>
					<view class="u-flex " style="color: #EA4D44;align-items: baseline;">
						<block v-if="item.score > 0">
							<view style="font-size: 46rpx;font-weight: bold;">{{item.score}}</view>
							<view class="u-f-24">积分</view>
							<view class="u-f-24">+</view>
						</block>

						<view style="font-size: 46rpx;font-weight: bold;">{{item.price}}</view>
						<view class="u-f-24">元</view>
					</view>
				</view>
				
			</view>
			<u-gap height="20rpx"></u-gap>
			<view class="u-flex-y-center u-flex-between">
				<view class="u-f-28">数量</view>
				<u-number-box v-model="item.number" @change="(value)=>{valChange(value,index)}" min="1"></u-number-box>
			</view>
			</block>
			
			<!-- <u-line margin="30rpx 0" color="#EFEFEF"></u-line> -->
			<!-- <view class="u-flex-y-center u-flex-between">
				<view class="u-f-28">配送服务</view>
				<view class="u-f-28" style="color: #666;">七天无理由退换·运费险</view>
			</view>
			<u-line margin="30rpx 0" color="#EFEFEF"></u-line>
			<view class="u-flex-y-center u-flex-between">
				<view class="u-f-28">等级优惠</view>
				<view class="u-f-28" style="color: #666;">黑卡会员下单享96折</view>
			</view> -->
			<u-line margin="30rpx 0" color="#EFEFEF"></u-line>
			<view class="u-flex-y-center u-flex-between" @click="show=true">
				<view class="u-f-28">订单备注</view>
				<view class="u-flex-y-center">
					<view class="u-f-28" style="color: #999;">{{remark||'填写备注'}}</view>
					<u-icon name="arrow-right"></u-icon>
				</view>
			</view>
		</view>
		<u-gap height="30rpx"></u-gap>
		<view class="u-r-16 u-bg-fff u-p-30">
			<view class="u-flex-y-center u-flex-between">
				<view class="u-f-28">商品金额</view>
				<view class="u-f-28 u-bold">¥{{cart.old_price}}</view>
			</view>
			<u-gap height="30rpx"></u-gap>
			<view class="u-flex-y-center u-flex-between">
				<view class="u-f-28">商品件数</view>
				<view class="u-f-28">{{cart.number}}件</view>
			</view>
			<u-gap height="30rpx"></u-gap>
			<view class="u-flex-y-center u-flex-between">
				<view class="u-f-28">优惠总额</view>
				<view class="u-f-28">¥{{cart.discount_amount}}</view>
			</view>
			<u-gap height="30rpx"></u-gap>
			<view class="u-flex-y-center u-flex-between">
				<view class="u-f-28">运费</view>
				<view class="u-f-28">¥{{cart.shipfee}}</view>
			</view>
			<u-gap height="30rpx"></u-gap>
			<view class="u-flex-y-center u-flex-between">
				<view class="u-f-28">合计</view>
				<view class="u-f-28">¥{{cart.amount}}</view>
			</view>
		</view>
		<u-gap height="200rpx"></u-gap>
		<view class="bottom">
			<view>
				<text class="u-f-24">总计金额：</text>
				<text class="u-f-24" style="color: #ED361D;">¥</text><text class="u-f-36 u-bold"  style="color: #ED361D;">{{cart.amount}}</text>
			</view>
			<view class="appBtn" style="width: 240rpx;" @click="sendPay">立即支付</view>
		</view>
		<u-popup :show="show" @close="show=false" closeable round="30rpx">
			<view style="padding: 40rpx 30rpx;">
				<view class="u-f-28 u-bold u-text-center">填写备注</view>
				<u-gap height="40rpx"></u-gap>
				<textarea class="u-r-16 u-p-20 u-f-28" style="background-color: #f8f8f8;width: 100%;" placeholder="选填，请先和商家协商一致，付款后商家可见。" v-model="remark"></textarea>
				<u-gap height="70rpx"></u-gap>
				<view class="appBtn" @click="show=false">确认</view>
			</view>
			
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				show:false,
				shop_id:0,
				attr_id:'',
				number:1,
				cart:[],
				address_id:0,
				remark:''
			}
		},
		onLoad(opt) {
			this.shop_id = opt.shop_id
			this.attr_id = opt.attr_id
			this.number = opt.number
			this.getInfo()
		},
		onShow() {
			if(this.address_id){
					this.getInfo()
			}
		},
		methods: {
			async valChange(m,e){
				if(this.shop_id){
					this.number = m.value
					this.getInfo()
					return 
				}
				let info = this.cart.cart[e]
				let res= await uni.$u.http.post('shop/cartAdd',{shop_id:info.shop_id,number:m.value,attr_id:info.attr_id});
				this.getInfo()
			},
			async getInfo(){
				let res= await uni.$u.http.post('order/orderSetting',{shop_id:this.shop_id,number:this.number,attr_id:this.attr_id,address_id:this.address_id});
				this.cart = res
				this.address_id = res.address.id
			},
			async sendPay(){
				let res= await uni.$u.http.post('order/submit',{shop_id:this.shop_id,number:this.number,attr_id:this.attr_id,address_id:this.address_id,remark:this.remark});
				this.pay(res.order_no, 1)
			},
			async pay(order_no, type) {
				let res = await uni.$u.http.post('order/pay', {
					order_no: order_no,
					type: type
				});
				let _this = this
				uni.requestPayment({
					"provider": "wxpay",
					timeStamp: res.timeStamp,
					nonceStr: res.nonceStr,
					package: res.package,
					signType: res.signType,
					paySign: res.paySign,
					success(res2) {
						uni.hideLoading()
						uni.showToast({
							title: '支付成功',
							icon: 'none'
						})
						setTimeout(function(){
							uni.reLaunch({
								url:'/pages/member/member'
							})
						},1000)
					},
					fail(e) {
						uni.hideLoading()
						uni.showToast({
							title: '支付失败,请重新支付',
							icon: 'none'
						})
					}
				})
			}
		}
	}
</script>

<style>
	page {
		background-color: #F8F8F8;
	}
	
	.label {
		padding: 0 30rpx;
		height: 54rpx;
		background: #F5F5F5;
		border-radius: 8rpx;
		line-height: 54rpx;
		color: #333333;
		font-size: 24rpx;
		margin-top: 20rpx;
		margin-right: 20rpx;
	}
	
	.labelAct {
		padding: 0 30rpx;
		height: 54rpx;
		background: #F6FAFF;
		border-radius: 8rpx;
		border: 1px solid #007FFF;
		line-height: 54rpx;
		color: #007FFF;
		font-size: 24rpx;
		margin-top: 20rpx;
		margin-right: 20rpx;
	}
	.bottom{
		padding: 30rpx;
		min-height: 100rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		background-color: #fff;
		position: fixed;
		width: 100%;
		bottom: 0;
		left: 0;
	}
</style>